package com.itheima;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.data.category.DefaultCategoryDataset;

import java.awt.*;
import java.io.File;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2022/12/17 11:50
 */

public class JfreeChartsDemo3 {

    public static void main(String[] args) throws Exception {
        //构建柱状图的数据集
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(15, "技术部", "2011");
        dataset.addValue(11, "技术部", "2012");
        dataset.addValue(10, "技术部", "2013");
        dataset.addValue(16, "技术部", "2014");

        dataset.addValue(10, "销售部", "2011");
        dataset.addValue(30, "销售部", "2012");
        dataset.addValue(6, "销售部", "2013");
        dataset.addValue(16, "销售部", "2014");

        dataset.addValue(10, "产品部", "2011");
        dataset.addValue(20, "产品部", "2012");
        dataset.addValue(30, "产品部", "2013");
        dataset.addValue(15, "产品部", "2014");

        //解决中文乱码问题
        StandardChartTheme theme = new StandardChartTheme("CN");
        //设置标题字体
        theme.setExtraLargeFont(new Font("华文宋体", Font.BOLD, 20));
        //设置图例字体
        theme.setRegularFont(new Font("华文宋体", Font.BOLD, 15));
        //设置轴向字体
        theme.setLargeFont(new Font("华文宋体", Font.BOLD, 15));
        //应用主题样式
        ChartFactory.setChartTheme(theme);

        //参数1  title 标题
        //参数2　x轴的说明
        //参数3   Y轴的说明
        //参数4   数据集
        JFreeChart jFreeChart = ChartFactory.createBarChart("各部门人数", "年度", "人数", dataset);

        //输出
        ChartUtils.saveChartAsJPEG(new File("d:\\charts3.jpg"), jFreeChart, 400, 300);

    }
}

